package com.itrus.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Hashtable;
import java.util.StringTokenizer;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LdapUtils
{
  private static Log log = LogFactory.getLog(LdapUtils.class);

  static String AddLeadingZero(int paramInt)
  {
    if (paramInt <= 15)
      return "0" + Integer.toHexString(paramInt);
    return Integer.toHexString(paramInt);
  }

  public static String escapeDN(String paramString)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    if ((paramString.length() > 0) && ((paramString.charAt(0) == ' ') || (paramString.charAt(0) == '#')))
      localStringBuilder.append('\\');
    int i = 0;
    if (i >= paramString.length())
    {
      if ((paramString.length() > 1) && (paramString.charAt(-1 + paramString.length()) == ' '))
        localStringBuilder.insert(-1 + localStringBuilder.length(), '\\');
      return localStringBuilder.toString();
    }
    char c = paramString.charAt(i);
    switch (c)
    {
    default:
      localStringBuilder.append(c);
    case '\\':
    case ',':
    case '+':
    case '"':
    case '<':
    case '>':
    case ';':
    }
    while (true)
    {
      i++;
      break;
      localStringBuilder.append("\\\\");
      continue;
      localStringBuilder.append("\\,");
      continue;
      localStringBuilder.append("\\+");
      continue;
      localStringBuilder.append("\\\"");
      continue;
      localStringBuilder.append("\\<");
      continue;
      localStringBuilder.append("\\>");
      continue;
      localStringBuilder.append("\\;");
    }
  }

  public static final String escapeLDAPSearchFilter(String paramString)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    int i = 0;
    if (i >= paramString.length())
      return localStringBuilder.toString();
    char c = paramString.charAt(i);
    switch (c)
    {
    default:
      localStringBuilder.append(c);
    case '\\':
    case '*':
    case '(':
    case ')':
    case '\000':
    }
    while (true)
    {
      i++;
      break;
      localStringBuilder.append("\\5c");
      continue;
      localStringBuilder.append("\\2a");
      continue;
      localStringBuilder.append("\\28");
      continue;
      localStringBuilder.append("\\29");
      continue;
      localStringBuilder.append("\\00");
    }
  }

  public static Attribute getEntryAttribute(String paramString1, String paramString2)
    throws NamingException
  {
    return getEntryAttributes(paramString1, new String[] { paramString2 }).get(paramString2);
  }

  public static Attributes getEntryAttributes(String paramString, String[] paramArrayOfString)
    throws NamingException
  {
    Object localObject = RegexUtils.exceptMatches(paramString, "^ldap://.*/");
    log.debug("Entry DN=[" + (String)localObject + "]");
    String str1 = RegexUtils.exceptMatches(paramString, "^ldap://|/.*");
    String str2 = "389";
    String str5;
    String str4;
    String str8;
    String str3;
    if (str1.indexOf("@") > 0)
    {
      String str7 = RegexUtils.exceptMatches(str1, "@.*");
      log.debug("Ldap Credentials=[" + str7 + "]");
      str5 = RegexUtils.exceptMatches(str7, ":.*");
      log.debug("Ldap User=[" + str5 + "]");
      str4 = RegexUtils.exceptMatches(str7, "^" + str5 + ":");
      log.debug("Ldap Password=[" + str4 + "]");
      str8 = RegexUtils.exceptMatches(str1, ".*@");
      log.debug("Ldap Server Url=[ldap://" + str8 + "]");
      if (str8.indexOf(":") > 0)
      {
        str3 = RegexUtils.exceptMatches(str8, ":.*");
        log.debug("Ldap Server IP=[" + str3 + "]");
        str2 = RegexUtils.exceptMatches(str8, ".*:");
        log.debug("Ldap Server Port=[" + str2 + "]");
      }
    }
    label387: Attributes localAttributes;
    while (true)
    {
      Hashtable localHashtable = new Hashtable();
      localHashtable.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
      localHashtable.put("java.naming.provider.url", "ldap://" + str3 + ":" + str2);
      InitialLdapContext localInitialLdapContext;
      if (str5 == null)
      {
        localHashtable.put("java.naming.security.authentication", "none");
        localInitialLdapContext = new InitialLdapContext(localHashtable, null);
      }
      try
      {
        String str6 = URLDecoder.decode((String)localObject, "GBK");
        localObject = str6;
        localAttributes = localInitialLdapContext.getAttributes((String)localObject, paramArrayOfString);
        if (localInitialLdapContext == null);
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException)
      {
        try
        {
          localInitialLdapContext.close();
          return localAttributes;
          str3 = str8;
          log.debug("Ldap Server IP=[" + str3 + "]");
          log.debug("Ldap Server Port=[" + str2 + "]");
          continue;
          log.debug("Ldap User=[" + null + "]");
          log.debug("Ldap Password=[" + null + "]");
          log.debug("Ldap Server Url=[ldap://" + str1 + "]");
          if (str1.indexOf(":") > 0)
          {
            str3 = RegexUtils.exceptMatches(str1, ":.*");
            log.debug("Ldap Server IP=[" + str3 + "]");
            str2 = RegexUtils.exceptMatches(str1, ".*:");
            log.debug("Ldap Server Port=[" + str2 + "]");
            str4 = null;
            str5 = null;
          }
          else
          {
            str3 = str1;
            log.debug("Ldap Server IP=[" + str3 + "]");
            str2 = "389";
            log.debug("Ldap Server Port=[" + str2 + "]");
            str4 = null;
            str5 = null;
            continue;
            localHashtable.put("java.naming.security.authentication", "simple");
            localHashtable.put("java.naming.security.principal", str5);
            localHashtable.put("java.naming.security.credentials", str4);
            break label387;
            localUnsupportedEncodingException = localUnsupportedEncodingException;
            localUnsupportedEncodingException.printStackTrace();
          }
        }
        catch (NamingException localNamingException)
        {
        }
      }
    }
    return localAttributes;
  }

  public static byte[] getEntryBinaryAttr(String paramString1, String paramString2)
    throws NamingException
  {
    return (byte[])getEntryAttribute(paramString1, paramString2).get();
  }

  public static String getEntryStringAttr(String paramString1, String paramString2)
    throws NamingException
  {
    return (String)getEntryAttribute(paramString1, paramString2).get();
  }

  public static String guidToHexString(String paramString)
  {
    if (paramString == null)
      throw new NullPointerException("Parameter byte[] GUID is null");
    if (paramString.startsWith("{"))
      paramString = paramString.substring(1);
    if (paramString.endsWith("}"))
      paramString = paramString.substring(0, -1 + paramString.length());
    StringBuffer localStringBuffer = new StringBuffer();
    StringTokenizer localStringTokenizer = new StringTokenizer(paramString, "-");
    Object localObject1 = null;
    Object localObject2 = null;
    Object localObject3 = null;
    Object localObject4 = null;
    Object localObject5 = null;
    int i = 1;
    if (!localStringTokenizer.hasMoreTokens())
    {
      localStringBuffer.append(localObject5.substring(6, 8));
      localStringBuffer.append(localObject5.substring(4, 6));
      localStringBuffer.append(localObject5.substring(2, 4));
      localStringBuffer.append(localObject5.substring(0, 2));
      localStringBuffer.append(localObject4.substring(2, 4));
      localStringBuffer.append(localObject4.substring(0, 2));
      localStringBuffer.append(localObject3.substring(2, 4));
      localStringBuffer.append(localObject3.substring(0, 2));
      localStringBuffer.append(localObject2.substring(0, 2));
      localStringBuffer.append(localObject2.substring(2, 4));
      localStringBuffer.append(localObject1.substring(0, 2));
      localStringBuffer.append(localObject1.substring(2, 4));
      localStringBuffer.append(localObject1.substring(4, 6));
      localStringBuffer.append(localObject1.substring(6, 8));
      localStringBuffer.append(localObject1.substring(8, 10));
      localStringBuffer.append(localObject1.substring(10, 12));
      return localStringBuffer.toString();
    }
    String str = localStringTokenizer.nextToken();
    switch (i)
    {
    default:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    }
    while (true)
    {
      i++;
      break;
      localObject5 = str;
      continue;
      localObject4 = str;
      continue;
      localObject3 = str;
      continue;
      localObject2 = str;
      continue;
      localObject1 = str;
    }
  }

  public static String guidToQueryString(String paramString)
  {
    if (paramString == null)
      throw new NullPointerException("Parameter byte[] GUID is null");
    if (paramString.startsWith("{"))
      paramString = paramString.substring(1);
    if (paramString.endsWith("}"))
      paramString = paramString.substring(0, -1 + paramString.length());
    StringBuffer localStringBuffer = new StringBuffer();
    StringTokenizer localStringTokenizer = new StringTokenizer(paramString, "-");
    Object localObject1 = null;
    Object localObject2 = null;
    Object localObject3 = null;
    Object localObject4 = null;
    Object localObject5 = null;
    int i = 1;
    if (!localStringTokenizer.hasMoreTokens())
    {
      localStringBuffer.append("\\" + localObject5.substring(6, 8));
      localStringBuffer.append("\\" + localObject5.substring(4, 6));
      localStringBuffer.append("\\" + localObject5.substring(2, 4));
      localStringBuffer.append("\\" + localObject5.substring(0, 2));
      localStringBuffer.append("\\" + localObject4.substring(2, 4));
      localStringBuffer.append("\\" + localObject4.substring(0, 2));
      localStringBuffer.append("\\" + localObject3.substring(2, 4));
      localStringBuffer.append("\\" + localObject3.substring(0, 2));
      localStringBuffer.append("\\" + localObject2.substring(0, 2));
      localStringBuffer.append("\\" + localObject2.substring(2, 4));
      localStringBuffer.append("\\" + localObject1.substring(0, 2));
      localStringBuffer.append("\\" + localObject1.substring(2, 4));
      localStringBuffer.append("\\" + localObject1.substring(4, 6));
      localStringBuffer.append("\\" + localObject1.substring(6, 8));
      localStringBuffer.append("\\" + localObject1.substring(8, 10));
      localStringBuffer.append("\\" + localObject1.substring(10, 12));
      return localStringBuffer.toString();
    }
    String str = localStringTokenizer.nextToken();
    switch (i)
    {
    default:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    }
    while (true)
    {
      i++;
      break;
      localObject5 = str;
      continue;
      localObject4 = str;
      continue;
      localObject3 = str;
      continue;
      localObject2 = str;
      continue;
      localObject1 = str;
    }
  }

  public static String parseGUID(byte[] paramArrayOfByte)
    throws IOException
  {
    if (paramArrayOfByte == null)
      throw new NullPointerException("Parameter byte[] GUID is null");
    if (paramArrayOfByte.length != 16)
      throw new IOException("Parameter byte[] GUID is not 16 bytes.");
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[3]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[2]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[1]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[0]));
    localStringBuffer.append("-");
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[5]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[4]));
    localStringBuffer.append("-");
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[7]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[6]));
    localStringBuffer.append("-");
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[8]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[9]));
    localStringBuffer.append("-");
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[10]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[11]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[12]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[13]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[14]));
    localStringBuffer.append(AddLeadingZero(0xFF & paramArrayOfByte[15]));
    return localStringBuffer.toString().toUpperCase();
  }

  public static byte[] parseGUID(String paramString)
    throws IOException
  {
    if (paramString == null)
      throw new NullPointerException("Parameter byte[] GUID is null");
    if (paramString.startsWith("{"))
      paramString = paramString.substring(1);
    if (paramString.endsWith("}"))
      paramString = paramString.substring(0, -1 + paramString.length());
    return DERUtils.HexToBytes(guidToHexString(paramString));
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.itrus.util.LdapUtils
 * JD-Core Version:    0.6.2
 */